package org.leyi.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.leyi.shop.mapper.SellerMapper;
import org.leyi.shop.mapper.UserMapper;
import org.leyi.shop.model.pojo.Seller;
import org.leyi.shop.model.pojo.User;
import org.leyi.shop.model.vo.SafetyUser;
import org.leyi.shop.service.SellerService;
import org.leyi.shop.service.UserService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author lenovo
* description 针对表【seller(用户表)】的数据库操作Service实现
* create 2024-05-04 23:18:59
*/
@Service
public class SellerServiceImpl extends ServiceImpl<SellerMapper, Seller>
    implements SellerService{
    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private SellerMapper sellerMapper;
  @Override
  public SafetyUser login(String userAccount, String userPassword, HttpServletRequest request) {
      //商家端没有注册，直接就是登录
      User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_account", userAccount));
      long id;
      if(user==null||user.getId()==null||user.getId()<=0)
      {
          //未注册
          id=userService.register(userAccount,userPassword);
      }
      else{
          id=user.getId();
      }
      //若在商家表没有相应字段
      long count = this.count(new QueryWrapper<Seller>().eq("user_account", userAccount));
      if(count==0)
      {
          Seller seller = new Seller();
          seller.setId(id);
          seller.setSellerAccount(userAccount);
          seller.setProfitAmount(0.0D);
          seller.setCreateTime(new Date());
          seller.setUpdateTime(new Date());
          seller.setDeleteLogic(0);
          this.save(seller);
      }
      //登录
      return userService.login(userAccount,userPassword,request);
  }
  @Override
    public double getProfit(String userAccount)
  {
      Seller seller= sellerMapper.selectOne(new QueryWrapper<Seller>().eq("seller_account", userAccount));
      return seller.getProfitAmount();
  }
}




